﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LL.Business;
using NanoERP.Model;
using NanoERP.Common;

namespace NanoERP.Business.BaseInfo
{
    /// <summary>
    /// 收付款账户信息业务类
    /// </summary>
    public class AccountinfoBLL : ServiceBase
    {
        /// <summary>
        /// 收付款账户信息查询
        /// </summary>
        /// <param name="accountTypeName">账户类别</param>
        /// <param name="query">查询关键字 支持：名称|拼音码</param>
        /// <returns></returns>
        public List<Accountinfo> Query(string accountTypeName, string query)
        {
            var sql = string.Format(@"('{0}'='全部账户' OR ACCOUNTTYPENAME='{0}') AND ('{1}'='' OR ACCOUNTNAME like '%{1}%' OR ACCOUNTPYM like '%{1}%') ", accountTypeName, query);

            var list = DbSession.Query<Accountinfo>(sql).ToList();

            var accSubBLL = new AccountsubjectBLL();
            list.ForEach(r =>
            {
                if (r.Subjectid != null)
                {
                    var obj = accSubBLL.Get(r.Subjectid.Value);
                    if (obj != null)
                    {
                        r.SubjectName = obj.Subjectname;
                        r.SubjectNo = obj.Subjectno;
                    }
                }
            });
            return list;
        }
        
        /// <summary>
        /// 收付款账户信息查询
        /// </summary>
        /// <returns></returns>
        public List<Accountinfo> Query()
        {
            return DbSession.GetAll<Accountinfo>().ToList();
        }

        /// <summary>
        /// 收付款账户信息
        /// </summary>
        /// <param name="id">收付款账户名称关键字</param>
        /// <returns></returns>
        public Accountinfo GetAccountByName(string name)
        {
            Accountinfo acc;
            List<Accountinfo> list = Query();
            foreach (Accountinfo account in list)
            {
                if (account.Accountname.Contains(name))
                {
                   return acc= account;                  
                }
            }
            return new Accountinfo() { Id=-1};
        }
        public Accountinfo Default
        {
            get
            {
                return GetAccountByName("现金");
            }
        }
        /// <summary>
        /// 收付款账户信息
        /// </summary>
        /// <param name="id">收付款账户信息ID</param>
        /// <returns></returns>
        public Accountinfo Get(long id)
        {
            var o = DbSession.Get<Accountinfo>(id);
            if (o.Subjectid != null)
            {
                var obj = new AccountsubjectBLL().Get(o.Subjectid.Value);
                if (obj != null)
                {
                    o.SubjectName = obj.Subjectname;
                    o.SubjectNo = obj.Subjectno;
                }
            }
            return o;
        }
        /// <summary>
        /// 编辑收付款账户信息
        /// </summary>
        /// <param name="o"></param>
        public void Edit(Accountinfo o)
        {
            if (o.Id == 0)
                this.DbSession.Insert(o, "Id", "SubjectName", "SubjectNo");
            else
                this.DbSession.Update(o);
        }

        /// <summary>
        /// 删除收付款账户信息
        /// </summary>
        /// <param name="id"></param>
        public void Delete(long id)
        {
            this.DbSession.Delete<Accountinfo>(id);
        }

        /// <summary>
        /// 浏览记录 上一条 下一条
        /// </summary>
        /// <param name="id">浏览记录Id</param>
        /// <param name="type">类型 0-向上,1-向下</param>
        /// <param name="isLast">是否最后一条数据</param>
        /// <returns></returns>
        public Accountinfo PreRecord(long id, int type, ref bool isLast)
        {
            var obj = DbSession.Get<Accountinfo>(id);
            if (obj == null)
            {
                var dt = DbSession.ExcuteQuery(string.Format("select {0}(ID) as ID from dbo.Accountinfo", type.Equals(0) ? "MIN" : "MAX"));
                var lId = dt.Rows.Count > 0 ? Normal.ToLong(dt.Rows[0][0]) : 0;

                isLast = type.Equals(0) ? id <= lId + 1 : id >= lId - 1;

                var list = DbSession.SqlQuery<Accountinfo>(
                    string.Format("select top 1 * from dbo.Accountinfo where ID {1} {0} Order by Id {2}",
                    isLast ? lId + (type.Equals(0) ? 1 : -1) : id, type.Equals(0) ? "<" : ">", type.Equals(0) ? "desc" : "asc"));

                obj = list.First();
            }

            return obj;
        }
    }
}
